package com.pdstudio.alarm;

import java.util.Calendar;

import com.pdstudio.weather.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.app.AlarmManager;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;

public class WeatherAlarm extends Activity {

	private Button btnsettime = null;
	private Button btncity = null;
	// private AlarmManager alarmManager=null;
	Calendar cal = Calendar.getInstance();
	final int DIALOG_TIME = 0; // 设置对话框id

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.alarm_main);

		// alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
		btnsettime = (Button) findViewById(R.id.alarm_weather_settime);
		btncity = (Button) findViewById(R.id.alarm_weather_city);

		btnsettime.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				cal.setTimeInMillis(System.currentTimeMillis());
				int hour = cal.get(Calendar.HOUR_OF_DAY);
				int minute = cal.get(Calendar.MINUTE);
				new TimePickerDialog(WeatherAlarm.this,
						new TimePickerDialog.OnTimeSetListener() {

							public void onTimeSet(TimePicker view,
									int hourOfDay, int minute) {
								// TODO Auto-generated method stub
								cal.setTimeInMillis(System.currentTimeMillis());
								cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
								cal.set(Calendar.MINUTE, minute);
								cal.set(Calendar.SECOND, 0);
								cal.set(Calendar.MILLISECOND, 0);
								Intent intent = new Intent(WeatherAlarm.this,
										WeatherAlarmReceiver.class);
								PendingIntent pendingIntent = PendingIntent
										.getBroadcast(WeatherAlarm.this, 0,
												intent, 0);
								AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
								alarmManager.set(AlarmManager.RTC_WAKEUP,
										cal.getTimeInMillis(), pendingIntent);
								alarmManager.setRepeating(
										AlarmManager.RTC_WAKEUP,
										System.currentTimeMillis()
												+ (10 * 1000),
										(24 * 60 * 60 * 1000), pendingIntent);
								String tmps = "设置闹钟时间为" + hourOfDay + ":"
										+ minute;
								Toast.makeText(getApplicationContext(), tmps,
										Toast.LENGTH_LONG).show();
							}
						}, hour, minute, true).show();
			}
		});

		btncity.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(WeatherAlarm.this,
						WeatherAlarmReceiver.class);
				PendingIntent pendingIntent = PendingIntent.getBroadcast(
						WeatherAlarm.this, 0, intent, 0);
				AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
				alarmManager.cancel(pendingIntent);
				Toast.makeText(getApplicationContext(), "闹铃已取消",
						Toast.LENGTH_LONG).show();
			}
		});

	}

}
